package project02.service;

import project02.domain.CurrentUser;
import project02.domain.User;
import project02.view.IndexView;
import project02.view.LoginView;

import java.io.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Properties;

public class LoginService {



    //实现注册功能
    public static boolean register(String uid, String name) throws IOException, ParseException, ClassNotFoundException {
        Properties prop = new Properties();
        FileReader fr = new FileReader(UserListService.UID_NAME);
        prop.load(fr);

        //property是文件里面的键值对，uid是传递过来的值。
        String property = prop.getProperty(uid);
        fr.close();
        if (property!=null&&property.equals(name)){
            ObjectInputStream ins=new ObjectInputStream(new FileInputStream(UserListService.INFORMATION));
            ArrayList<User> list = (ArrayList<User>)ins.readObject();
            ins.close();
            for (User user : list) {
                if (user.getUid().equals(uid)){
                    System.out.println("该用户已注册");
                    return false;
                }

            }
            System.out.println("用户可注册，请输入用户信息。");

            return true;

        }else {
            System.out.println("输入信息有误，请联系管理员确认信息。");
           return false;


        }



    }

    //实现登录功能
    public boolean login(String uid, String password) throws IOException, ClassNotFoundException {
        ObjectInputStream jis = new ObjectInputStream(new FileInputStream(UserListService.INFORMATION));

        ArrayList<User> list  = (ArrayList<User>)jis.readObject();
        jis.close();
        for (User user : list) {

            if (user.getUid().equals(uid)&&user.getPassword().equals(password)){
                if (!user.isAlive()){
                    System.out.println("账户已经被禁用。");
                    return false;
                }
                CurrentUser.currentUser=user;
                return true;

            }

        }
        return false;
    }



    //实现修改用户名功能
    public void modifyUserInformation(String userName) throws IOException, ClassNotFoundException {
        Properties prop = new Properties();
        FileReader fr = new FileReader(UserListService.UID_NAME);
        prop.load(fr);
       prop.setProperty(CurrentUser.currentUser.getUid(),userName);
       fr.close();
       ObjectInputStream pp=new ObjectInputStream(new FileInputStream(UserListService.INFORMATION));
       ArrayList<User>ps=(ArrayList<User>)pp.readObject();
        for (User p : ps) {
            if (p.getUid().equals(CurrentUser.currentUser.getUid())){
                p.setName(userName);
                System.out.println("修改成功！");


            ObjectOutputStream ppp=new ObjectOutputStream(new FileOutputStream(UserListService.INFORMATION));
            ppp.writeObject(ps);
            ppp.flush();
            ppp.close();
                break;

        }else {
                System.out.println("修改失败！");
            }



    }
    }


    public void checkCreditRating(){
//        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(UserListService.INFORMATION));
//        Object obj = ois.readObject();

        User s = CurrentUser.currentUser;
            switch (s.getLevel()){
                case 0:
                    System.out.println("您的信用等级为0，无法申请兼职工作");
                    break;
                case 1:
                    System.out.println("您的信用等级为1，按时完成3次工作后信用等级恢复为2级。");
                    break;
                case 2:
                    System.out.println("您的信用等级为2，按时完成3次工作后信用等级恢复为3级。");
                    break;
                case 3:
                    System.out.println("您的信用等级为3，按时完成3次工作后信用等级恢复为4级。");
                    break;
                case 4:
                    System.out.println("您的信用等级为4，按时完成3次工作后信用等级恢复为5级。");
                    break;
                case 5:
                    System.out.println("您的信用等级为5，为最高信用等级，请继续保持！");
                    break;


            }



}

    public void modifyPassword(String newPassWord) throws IOException, ClassNotFoundException {
        ObjectInputStream pp = new ObjectInputStream(new FileInputStream(UserListService.INFORMATION));
        ArrayList<User> ps = (ArrayList<User>) pp.readObject();
        for (User p : ps) {
            if (p.getUid().equals(CurrentUser.currentUser.getUid())) {
                p.setPassword(newPassWord);
                System.out.println("修改成功！");
                ObjectOutputStream ppp = new ObjectOutputStream(new FileOutputStream(UserListService.INFORMATION));
                ppp.writeObject(ps);
                ppp.flush();
                ppp.close();
                break;

            } else {
                System.out.println("修改失败！");
            }

        }
    }

    public void modifySex(String newSex) throws IOException, ClassNotFoundException {
        ObjectInputStream pp = new ObjectInputStream(new FileInputStream(UserListService.INFORMATION));
        ArrayList<User> ps = (ArrayList<User>) pp.readObject();
        for (User p : ps) {
            if (p.getUid().equals(CurrentUser.currentUser.getUid())) {
                p.setSex(newSex);
                System.out.println("修改成功！");


                ObjectOutputStream ppp = new ObjectOutputStream(new FileOutputStream(UserListService.INFORMATION));
                ppp.writeObject(ps);
                ppp.flush();
                ppp.close();
                break;

            } else {
                System.out.println("修改失败！");
            }

        }

    }

    public void modifyDepartment(String dea) throws IOException, ClassNotFoundException {

        ObjectInputStream pp = new ObjectInputStream(new FileInputStream(UserListService.INFORMATION));
        ArrayList<User> ps = (ArrayList<User>) pp.readObject();
        for (User p : ps) {
            if (p.getUid().equals(CurrentUser.currentUser.getUid())) {
                p.setDepartment(dea);
                System.out.println("修改成功！");


                ObjectOutputStream ppp = new ObjectOutputStream(new FileOutputStream(UserListService.INFORMATION));
                ppp.writeObject(ps);
                ppp.flush();
                ppp.close();
                break;

            } else {
                System.out.println("修改失败！");
            }


        }
    }

    public void modifyBirthday(String newBirthday) throws IOException, ClassNotFoundException {
        ObjectInputStream pp = new ObjectInputStream(new FileInputStream(UserListService.INFORMATION));
        ArrayList<User> ps = (ArrayList<User>) pp.readObject();
        for (User p : ps) {
            if (p.getUid().equals(CurrentUser.currentUser.getUid())) {
                p.setBirthday(newBirthday);
                System.out.println("修改成功！");


                ObjectOutputStream ppp = new ObjectOutputStream(new FileOutputStream(UserListService.INFORMATION));
                ppp.writeObject(ps);
                ppp.flush();
                ppp.close();
                break;

            } else {
                System.out.println("修改失败！");
            }

        }
}

    public void modifyAge(Integer newAge) throws IOException, ClassNotFoundException {
        ObjectInputStream pp = new ObjectInputStream(new FileInputStream(UserListService.INFORMATION));
        ArrayList<User> ps = (ArrayList<User>) pp.readObject();
        for (User p : ps) {
            if (p.getUid().equals(CurrentUser.currentUser.getUid())) {
                p.setAge(newAge);
                System.out.println("修改成功！");


                ObjectOutputStream ppp = new ObjectOutputStream(new FileOutputStream(UserListService.INFORMATION));
                ppp.writeObject(ps);
                ppp.flush();
                ppp.close();
                break;

            } else {
                System.out.println("修改失败！");
            }

        }
    }

    public void modifyPhone(String newNumber) throws IOException, ClassNotFoundException {
        ObjectInputStream pp = new ObjectInputStream(new FileInputStream(UserListService.INFORMATION));
        ArrayList<User> ps = (ArrayList<User>) pp.readObject();
        for (User p : ps) {
            if (p.getUid().equals(CurrentUser.currentUser.getUid())) {
                p.setPhoneNumber(newNumber);
                System.out.println("修改成功！");


                ObjectOutputStream ppp = new ObjectOutputStream(new FileOutputStream(UserListService.INFORMATION));
                ppp.writeObject(ps);
                ppp.flush();
                ppp.close();
                break;

            } else {
                System.out.println("修改失败！");
            }


        }
    }
}


